package springboot.springboot06.Controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;

import org.springframework.beans.factory.annotation.Autowired;

import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;
import springboot.springboot06.Bean.User;
import springboot.springboot06.Service.UserService;

import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.util.List;

@Controller
public class tableController {

    @Autowired
    UserService userService;

    @GetMapping("/basic_table")
    public String basic_table(){
        return "table/basic_table";
    }

    @GetMapping("/user/add")
    public String addUser(@RequestParam("total") Integer total,
                             @RequestParam("name") String name,
                             @RequestParam("age") int age,
                             @RequestParam("email") String email,
                             @RequestParam(value="pn", defaultValue = "100") int pn,
                             RedirectAttributes ra){

        Long id = Long.valueOf(total);
        id=id+1L;
        User user = new User();
        user.setId(id);
        user.setName(name);
        user.setAge(age);
        user.setEmail(email);

        userService.save(user);
        ra.addAttribute("pn",pn);
        return "redirect:/dynamic_table";
    }

    @GetMapping("/user/update")
    public String updateUser(@RequestParam("id") Long id,
                             @RequestParam("name") String name,
                             @RequestParam("age") int age,
                             @RequestParam("email") String email,
                             @RequestParam(value="pn", defaultValue = "1") int pn,
                             RedirectAttributes ra){

//        System.out.println(id+name+age+email);

        User user = new User();
        user.setId(id);
        user.setName(name);
        user.setAge(age);
        user.setEmail(email);

        userService.updateById(user);

        ra.addAttribute("pn",pn);
        return "redirect:/dynamic_table";
    }

    @ResponseBody
    @GetMapping("/user/query")
    public User queryUser(HttpServletResponse response,
                          @RequestParam("id") Integer id,
                          @RequestParam(value="pn", defaultValue = "1") int pn,
                          Model model) throws IOException {
        User user = userService.getById(id);

        model.addAttribute("userq",user);
//        model.addAttribute("pn", pn);
        System.out.println(user);

        return user;
    }

    @GetMapping("/user/delete/{id}")
    public String deleteUser(@PathVariable("id") Long id,
                             @RequestParam(value="pn", defaultValue = "1") int pn,
                             RedirectAttributes ra){

        userService.removeById(id);
        ra.addAttribute("pn",pn);
        return "redirect:/dynamic_table";
    }


    @GetMapping("/dynamic_table")
    public String dynamic_table(@RequestParam(value="pn" ,defaultValue = "1")Long pn,
                                Model model){
        List<User> list = userService.list();

        model.addAttribute("users",list);

        //分页查询的数据
        Page<User> userPage = new Page<>(pn,2);
        //分页查询的结果
        Page<User> page = userService.page(userPage,null);

//        Long current = page.getCurrent();
          Long pages = page.getPages();
//        session.setAttribute("pages",pages);
//        long total = page.getTotal();

        if(pn>pages){
            pn =  pages;
        }

        //分页查询的数据
        Page<User> userPage2 = new Page<>(pn,2);
        //分页查询的结果
        Page<User> page2 = userService.page(userPage2,null);


        model.addAttribute("page",page2);

        return "table/dynamic_table";
    }

    @GetMapping("/responsive_table")
    public String responsive_table(){
        return "table/responsive_table";
    }

    @GetMapping("/editable_table")
    public String editable_table(){
        return "table/editable_table";
    }

}
